package com.zd.kids.ui.adapter;

import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.SizeUtils;
import com.blankj.utilcode.util.StringUtils;
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.zd.kids.DataBean.BookUnitDataBean;
import com.zd.kids.R;
import com.zd.kids.common.GlideUtil;
import com.zd.kids.common.ToastUtils;
import com.zd.kids.ui.activity.BookSeriesListActivity;
import com.zd.kids.ui.activity.TestTrainActivity;
import com.zd.kids.ui.activity.TrainTypeListActivity;

import java.util.List;

/**
 * @ClassName: com.zd.kids.ui.adapter
 * @Description:
 * @Author: hyy
 * @Date: 2020/6/14
 * @Time: 5:33 PM
 */
public class BookUnitListAdapter extends BaseMultiItemQuickAdapter<BookUnitDataBean.ResultBean, BaseViewHolder> {
    /**
     * Same as QuickAdapter#QuickAdapter(Context,int) but with
     * some initialization data.
     *
     * @param data A new list is created out of this one to avoid mutable list
     */
    public BookUnitListAdapter(List<BookUnitDataBean.ResultBean> data) {
        super(data);

        addItemType(0, R.layout.activity_book_unit_item);

        addItemType(1, R.layout.activity_book_unit_ai_item);
    }

    public void setmGroupid(String mGroupid) {
        this.mGroupid = mGroupid;
    }

    public void setmCategoryid(String mCategoryid) {
        this.mCategoryid = mCategoryid;
    }

    private String mGroupid;

    private String mCategoryid;

    @Override
    protected void convert(BaseViewHolder helper, BookUnitDataBean.ResultBean item) {
        ImageView img_top = helper.getView(R.id.img_top);
        LinearLayout ll_complete = helper.getView(R.id.ll_complete);

        TextView tv_positon = helper.getView(R.id.tv_positon);
        if (tv_positon != null) {
            tv_positon.setText(item.getNumber());
        }

        TextView tv_title = helper.getView(R.id.tv_title);
        if (tv_title != null) {
            tv_title.setText(item.getTitle());
        }


        LinearLayout ll_ai_content_des = helper.getView(R.id.ll_content_des);
        RelativeLayout rl_ai_lock = helper.getView(R.id.rl_lock);
        TextView tv_ai_status = helper.getView(R.id.tv_status);
        TextView tv_name = helper.getView(R.id.tv_name);

        LinearLayout ll_not_complete = helper.getView(R.id.ll_not_complete);

        ProgressBar progress_bar = helper.getView(R.id.progress_bar);

        if (ll_complete != null) {
            ll_complete.setVisibility(View.GONE);
        }

        if (ll_not_complete != null) {
            ll_not_complete.setVisibility(View.GONE);
        }

        if (item.getPercent() == 100) {
            if (ll_complete != null) {
                ll_complete.setVisibility(View.VISIBLE);
            }
        } else {
            if (ll_complete != null) {
                ll_not_complete.setVisibility(View.VISIBLE);
                progress_bar.setProgress(item.getPercent());
            }
        }

        if (img_top != null) {
            GlideUtil.loadCeterCrop(item.getTitleimage(), img_top, SizeUtils.dp2px(10), GlideUtil.RoundType.Top);
        }

        View root_view = helper.getView(R.id.root_view);

        root_view.setOnClickListener(view -> {
            if (item.getItemType() == 1) {
                if ("1".equals(item.getAssessState())) {
                    Intent intent = new Intent(mContext, TestTrainActivity.class);
                    intent.putExtra(TestTrainActivity.AI_TEST_KEY, true);
                    intent.putExtra(BookSeriesListActivity.GROUP_ID_KEY, mGroupid);
                    intent.putExtra(BookSeriesListActivity.CATEGORY_ID_KEY, mCategoryid);
                    mContext.startActivity(intent);
                } else if ("2".equals(item.getAssessState())) {
                    ToastUtils.getIntence().makeTextShow(StringUtils.getString(R.string.please_contact_assess));
                } else if ("3".equals(item.getAssessState())) {
                    ToastUtils.getIntence().makeTextShow(StringUtils.getString(R.string.test_complete));
                } else {
//                    ToastUtils.getIntence().makeTextShow("还未解锁继续努力哦");
                }
            } else {
                Intent intent = new Intent(mContext, TrainTypeListActivity.class);
                intent.putExtra(TrainTypeListActivity.UNIT_ID_KEY, item.getId());
                intent.putExtra("Groupid", mGroupid);
                mContext.startActivity(intent);
            }
        });


        if (item.getItemType() == 1) {
            if ("0".equals(item.getAssessState())) {
                ll_ai_content_des.setVisibility(View.GONE);
                rl_ai_lock.setVisibility(View.VISIBLE);
            } else {
                ll_ai_content_des.setVisibility(View.VISIBLE);
                rl_ai_lock.setVisibility(View.GONE);

                tv_name.setText(StringUtils.getString(R.string.ai_assess));
                if ("1".equals(item.getAssessState())) {
                    tv_ai_status.setText(StringUtils.getString(R.string.now_start));
                } else if ("2".equals(item.getAssessState())) {
                    tv_ai_status.setText(StringUtils.getString(R.string.please_reservation_two));
                    tv_name.setText(StringUtils.getString(R.string.assess_complete_one));
                } else if ("3".equals(item.getAssessState())) {
                    tv_ai_status.setText(StringUtils.getString(R.string.completed));
                }
                tv_name.setText(StringUtils.getString(R.string.ai_assess));
                tv_ai_status.setText(StringUtils.getString(R.string.now_start));
            }
        }

    }
}
